package com.song.day01protobuf编码解码.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

import java.io.FileNotFoundException;
import java.io.IOException;

import com.song.day01protobuf编码解码.PackageMessageProto;

public class Client {

	private EventLoopGroup group ;
	
	private ChannelFuture future;
	
	private ClientHandler handler;
	
	
	public void  connect(String addr , int port)
	{
		EventLoopGroup group = new NioEventLoopGroup();
		this.group = group;
		this.handler = new ClientHandler();
		Bootstrap bootstrap = new Bootstrap();
		bootstrap.group(group).channel(NioSocketChannel.class)
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)
		.handler(new ChannelInitializer<SocketChannel>() {
					@Override
					protected void initChannel(SocketChannel ch)
							throws Exception {
						ChannelPipeline p = ch.pipeline();
						p.addLast(new ProtobufVarint32FrameDecoder());
						p.addLast(new ProtobufDecoder(PackageMessageProto.PackageMessage.getDefaultInstance()));
						p.addLast(new ProtobufVarint32LengthFieldPrepender());
						p.addLast(new ProtobufEncoder());
						p.addLast(handler);
					}

				});
		try {
			this.future = bootstrap.connect(addr, port).sync();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
//	public Object  sendAndRecv(MessageType messageType , String key ,Object val)
//	{
//		try {
//			PackageMessage msg = CoreUtils.dataToPackageMessage(messageType, key, val);
//			this.future.channel().writeAndFlush(msg).sync();
//			
//			while(true)
//			{
//				if(!this.handler.isEmpQueue())
//					break;
//			}
//			return this.handler.readMsg().body.bundle.get("data");
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//	}
	
	public void sendtest()
	{
		PackageMessageProto.PackageMessage.Builder builder =  PackageMessageProto.PackageMessage.newBuilder();
		builder.setAge(23);
		builder.setId(1);
		builder.setName("song");
		PackageMessageProto.PackageMessage msg = builder.build();
		try {
			this.future.channel().writeAndFlush(msg).sync();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void Release()
	{
		this.group.shutdownGracefully();
	}
	public static void main(String[] args) throws FileNotFoundException, IOException {
		Client client = new Client();
		client.connect("192.168.2.114", 58000);
		client.sendtest();
		client.Release();
		
	}
}
